package com.boil.qz.safekavass.model;

import com.boil.util.Constants;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by ChenYong on 2017-07-11.
 * <br>
 * 资源实体类。
 */
@Entity
@Table(name = "TB_QZ_RESOURCE")
public class ResourceEntity implements Comparable<ResourceEntity>, Serializable {
    /**
     * 序列化 ID
     */
    private static final long serialVersionUID = 6513399061301349690L;
    /**
     * 资源 ID
     */
    @Id
    @SequenceGenerator(name = "SgQzResource", sequenceName = "SE_QZ_RESOURCE", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SgQzResource")
    private long id;
    /**
     * 资源名称
     */
    private String resourceName;
    /**
     * 资源图标
     */
    private String resourceIcon;
    /**
     * 资源 URL
     */
    private String resourceUrl;
    /**
     * 资源类型：0-根资源；1-菜单；2-按钮……
     */
    private Integer resourceType;
    /**
     * 资源层级
     */
    private Integer resourceLevel;
    /**
     * 资源顺序
     */
    private Integer resourceOrder;
    /**
     * 权限代码
     */
    private String permissionCode;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 删除代码：0-未删除；1-已删除
     */
    private Integer deletedCode;
    /**
     * 父资源（懒加载）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_ID")
    private ResourceEntity parent;
    /**
     * 子资源（懒加载）
     */
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "parent")
    private Set<ResourceEntity> childs;
    /**
     * 资源拥有的角色 <code>Set</code>
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinTable(name = "TB_QZ_ROLE_RESOURCE", //
            joinColumns = @JoinColumn(name = "RESOURCE_ID"), //
            inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    private Set<RoleEntity> roles;

    // 普通初始化块
    {
        childs = new TreeSet<ResourceEntity>();
        roles = new HashSet<RoleEntity>();
    }

    /**
     * 默认构造器。
     */
    public ResourceEntity() {
        super();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public String getResourceIcon() {
        return resourceIcon;
    }

    public void setResourceIcon(String resourceIcon) {
        this.resourceIcon = resourceIcon;
    }

    public String getResourceUrl() {
        return resourceUrl;
    }

    public void setResourceUrl(String resourceUrl) {
        this.resourceUrl = resourceUrl;
    }

    public Integer getResourceType() {
        return resourceType;
    }

    public void setResourceType(Integer resourceType) {
        this.resourceType = resourceType;
    }

    public Integer getResourceLevel() {
        return resourceLevel;
    }

    public void setResourceLevel(Integer resourceLevel) {
        this.resourceLevel = resourceLevel;
    }

    public Integer getResourceOrder() {
        return resourceOrder;
    }

    public void setResourceOrder(Integer resourceOrder) {
        this.resourceOrder = resourceOrder;
    }

    public String getPermissionCode() {
        return permissionCode;
    }

    public void setPermissionCode(String permissionCode) {
        this.permissionCode = permissionCode;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getDeletedCode() {
        return deletedCode;
    }

    public void setDeletedCode(Integer deletedCode) {
        this.deletedCode = deletedCode;
    }

    public ResourceEntity getParent() {
        return parent;
    }

    public void setParent(ResourceEntity parent) {
        this.parent = parent;
    }

    public Set<ResourceEntity> getChilds() {
        return childs;
    }

    public void setChilds(Set<ResourceEntity> childs) {
        this.childs = new TreeSet<ResourceEntity>(childs);
    }

    public Set<RoleEntity> getRoles() {
        return roles;
    }

    public void setRoles(Set<RoleEntity> roles) {
        this.roles = roles;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ResourceEntity resource = (ResourceEntity) o;

        return id == resource.id;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("ResourceEntity{");
        sb.append("id=").append(id);
        sb.append(", resourceName='").append(resourceName).append('\'');
        sb.append(", resourceIcon='").append(resourceIcon).append('\'');
        sb.append(", resourceUrl='").append(resourceUrl).append('\'');
        sb.append(", resourceType=").append(resourceType);
        sb.append(", resourceLevel=").append(resourceLevel);
        sb.append(", resourceOrder=").append(resourceOrder);
        sb.append(", permissionCode='").append(permissionCode).append('\'');
        sb.append(", createTime=").append(createTime);
        sb.append(", deletedCode=").append(deletedCode);
        sb.append(", parent=").append(parent);
        sb.append(", childs=").append(childs);
        sb.append(", roles=").append(roles);
        sb.append('}');
        return sb.toString();
    }

    @Override
    public int compareTo(ResourceEntity o) {
        if ((this == null) || (o == null)) {
            return Constants.VALUE_0;
        }

        return this.getResourceOrder().compareTo(o.getResourceOrder());
    }
}